package com.zx.security.security;

import io.jsonwebtoken.CompressionCodecs;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class TokenManger {
    private long tokenEcpiration = 24*60*60*1000;
    private String tokenSingKey = "123456";
    //根据用户名生成token
    public String createToken(String username){
        String token = Jwts.builder()
                .setSubject(username)
                .setExpiration(new Date(System.currentTimeMillis()+tokenEcpiration))
                .signWith(SignatureAlgorithm.HS512,tokenSingKey).compressWith(CompressionCodecs.GZIP).compact();
        return token;
    }

    //根据token字符串获取信息

    public String getTokenInformation(String token){
        String userInfo = Jwts.parser().setSigningKey(tokenSingKey).parseClaimsJws(token).getBody().getSubject();
        return userInfo;
    }
}
